
#ifndef MY_GACCESSMESSAGE
#define MY_GACCESSMESSAGE

HRESULT  gAccessMessage(wstring regContent) {

	HRESULT hr = E_FAIL;
	int answer = MessageBoxW(
		GetDesktopWindow(),
		L"The typical solution to this problem is to restart application under administrator rights, but you can create .reg-file and import its manually (use standard tool regedit.exe under administration rights).",
		L"Access to the Registry Key Denied: How to Fix?",
		MB_OK | MB_ICONEXCLAMATION | MB_YESNOCANCEL);

	if (answer == IDYES) {
		
		OPENFILENAME ofn = {0};
		
		OSVERSIONINFO osver;
		osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
		GetVersionEx(&osver);

		if(osver.dwMajorVersion < 5 ) {
			ofn.lStructSize=OPENFILENAME_SIZE_VERSION_400 ;
		} else {
			ofn.lStructSize=sizeof (OPENFILENAME );
		}		
		
		ofn.hwndOwner = GetDesktopWindow();
		ofn.lpstrFilter = "Regedit Files (*.reg)\0*.reg\0All Files (*.*)\0*.*\0";

		char* buf = (char*)HeapAlloc(
			GetProcessHeap(), 
			HEAP_ZERO_MEMORY,
			MAX_PATH
		);
		
		strcpy(buf, "manual.reg");

	    ofn.lpstrFile = buf;
		ofn.nMaxFile = MAX_PATH;
		ofn.Flags = OFN_LONGNAMES | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
		ofn.lpstrDefExt = "reg";		
		
		if (GetSaveFileName(&ofn)) {
			HANDLE hFile;			
		    hFile = CreateFile(ofn.lpstrFile, GENERIC_WRITE, 0, NULL,
						        CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
		    if(hFile != INVALID_HANDLE_VALUE) {

		    	unsigned uLength = regContent.length();		    	
				char* p = (char*)HeapAlloc(
					GetProcessHeap(), 
					HEAP_ZERO_MEMORY,
					uLength + 1
				);
		    	WideCharToMultiByte(
					CP_ACP, 
					0, 
					regContent.c_str(), 
					uLength, 
					p, 
					uLength, 
					NULL, 
					NULL
				);
				DWORD dw;
				if (!WriteFile(
					hFile, 
					p, 
					strlen(p), 
					&dw, 
					NULL
				)) {
					gErrorMsgA(GetLastError(), ofn.lpstrFile);
				}
				HeapFree(GetProcessHeap(), 0, p);	
		    	CloseHandle(hFile);
		    } else {
		    	gErrorMsgA(GetLastError(), ofn.lpstrFile);
		    }

		}
		
		HeapFree(GetProcessHeap(), 0, buf);	

	};
	
	if (answer == IDNO) {
		size_t N = MAX_PATH * sizeof(WCHAR);
		
		PWSTR buf = (PWSTR)HeapAlloc(
			GetProcessHeap(), 
			HEAP_ZERO_MEMORY,
			N
		);
		
		DWORD res = GetModuleFileNameW(
			GetModuleHandle(NULL),
			buf, 
			MAX_PATH
		);
	
		if (res > 0) {
 
			wstring l_param;
			l_param.clear();
			int pos = regContent.find(L"[-", 0);
			if (pos >= 0) {
				l_param.append(L" /u ");
			};
			l_param.append(L"\"");
			l_param.append(g_params[g_idDLLPATH].c_str());
			l_param.append(L"\"");			
			
			SHELLEXECUTEINFOW sei = {0};
			sei.cbSize = sizeof(SHELLEXECUTEINFOW);
			sei.lpVerb = L"runas";
			sei.hwnd = GetDesktopWindow();
			sei.lpFile = buf;
			sei.lpParameters = l_param.c_str();
			sei.nShow = SW_SHOW;
		
			if (!ShellExecuteExW(&sei)) {
				gErrorMsgW(GetLastError(), buf, l_param.c_str(), NULL);	
			};
			
			
		};
		
		HeapFree(GetProcessHeap(), 0, buf);			
		
		
	}
	
	return hr;
}

#endif

